package pl.druminski.robinhood.controller;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;

/**
 *
 * @author Łukasz Drumiński
 *
 * Klasa sprawdza czy jest połączenie z Internetem
 */
public class InternetChecker
{

    private URL url = null;
    private URLConnection urlConnection = null;
    private InputStream inputStream = null;
    private BufferedReader bufferReader;
    private String contents = null;

    /**
     * 
     * @return jeśli jest połączenie z Internetem, to zwraca true, w przeciwnym przypadku zwraca false
     */
    public boolean checkConnection()
    {
        Boolean status = true;

        try
        {
            url = new URL("http://www.google.com");
            urlConnection = url.openConnection();
        } catch (Exception e)
        {
            status = false;
        }

        try
        {
            inputStream = urlConnection.getInputStream();
            bufferReader = new BufferedReader(new InputStreamReader(inputStream));

        } catch (Exception e)
        {
        }

        try
        {

            //testowanie polaczenia internetowego
            String s = "";
            contents = "";

            while ((s = bufferReader.readLine()) != null)
            {
                contents = contents.concat(s);

            }
            
            inputStream.close();

        } catch (Exception e)
        {
            status = false;
        }

        return status;

    }
}

